package sf.controller;

import javax.swing.table.TableModel;

public class MatchTableModelFilter {
	private int columns[];
	private String strings[];
	private TableModel tModel;
	
	public MatchTableModelFilter(int columns[], String strings[], TableModel t) {
		if (columns.length == strings.length && t!=null) {
			this.columns = columns;
			this.strings = strings;
			this.tModel = t;
		}
		else {
			// Exception ili neshto
		}
	}
	
	public MatchTableModelFilter(String columnNames[], String strings[], TableModel t) {
		if (columnNames.length == strings.length && t!=null) {
			columns = new int[columnNames.length];
			this.tModel = t;
			this.strings = strings;
			
			for (int j=0; j<t.getColumnCount(); j++) {			
				for (int i=0; i<columnNames.length; i++) {				
					if (t.getColumnName(j).equals(columnNames[i])) {
						columns[i] = j;
					}
				}
			}			
		}
		else {
			// Exception ili neshto
		}
	}
	
	public boolean accept(int row) {
		for (int i=0; i<columns.length; i++) {		
			if (!String.valueOf(tModel.getValueAt(row, columns[i])).equals(strings[i]))
				return false;
		}
		return true;
	}
	
	public int getMatchedIndex() {
		   for (int i=0; i<tModel.getRowCount(); i++) {
			   if (accept(i)) 
				   return i;				
		   }
		   return -1;
	}
	
	
}
